﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class ItemDAO
    {
        DBService _db = null;
        public ItemDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable InsUp_Items(Items items, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_Items_InUp",

                    new string[] {"@ItemID", "@ItemNumber", "@ItemName", "@ItemDescription", "@QuantityOnHand", "@ValueOnHand", 
                                  "@IsInActive", "@BaseSellingPrice", "@PriceIsInClusive", "@SellTaxCodeID", "@IDCateChild", "@Type", "@Color",
                                 "@Size", "@Discount", "@mode"},

                    new object[] { items.ItemID, items.ItemNumber, items.ItemName, items.ItemDescription, items.QuantityOnHand, items.ValueOnHand,
                                    items.IsInActive, items.BaseSellingPrice, items.PriceIsInClusive, items.SellTaxCodeID, items.IDCateChild, 
                                    items.Type, items.Color, items.Size, items.Discount, mode}, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetListItems(int ItemID, string p_ItemName, string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_Lst",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort" },
                    new object[] { ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetListItems_Admin(int ItemID, string p_ItemName, string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_Lst_Admin",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort" },
                    new object[] { ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // All Item_Admin ( khong tinh tiền bao gom thuế )

        public DataTable GetAllItems_Admin()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItem_Admin", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // All Item_Tinh Tien Bao Gom Thue
        public DataTable GetAllItems()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        // delete san pham
        public bool DeleteItem(int ItemID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete from Items where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // update Price

        public bool UpdatePrice(int ItemID, double Price)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update Items set BaseSellingPrice = @Price where ItemID = @ItemID",
                    new string[] { "@ItemID", "@Price" },
                    new object[] { ItemID, Price },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }


        // Update Active
        public bool Update_Active(int ItemID, string Active)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update Items set IsInActive = @Active where ItemID = @ItemID",
                    new string[] { "@ItemID", "@Active" },
                    new object[] { ItemID, Active },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }


        // Load Detail Item

        public Items LoadItem_ByID(int ItemID)
        {
            Items item = new Items();
            try
            {
                DataTable dt = null;
                dt = _db.ExeSP_DataTable("select * from vItem_Admin where ItemID = @ItemID", 
                    new string[]{ "@ItemID" },
                    new object[]{ ItemID }, CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ItemID = ItemID;

                    item.ItemName = dr["ItemName"].ToString();
                    item.ItemNumber = dr["ItemNumber"].ToString();
                    item.QuantityOnHand = Convert.ToInt32(dr["QuantityOnHand"]);
                    item.ValueOnHand = Convert.ToInt32(dr["ValueOnHand"]);
                    item.ItemDescription = dr["ItemDescription"].ToString();
                    item.IDCateChild = Convert.ToInt32(dr["IDCateChild"]);
                    item.Size = dr["Size"].ToString();
                    item.Color = dr["Color"].ToString();
                    item.BaseSellingPrice = Convert.ToDouble(dr["BaseSellingPrice"]);
                    item.Discount = float.Parse(dr["Discount"].ToString());
                    item.ImageURL = dr["ImageURL"].ToString();
                    item.Type = Convert.ToInt32(dr["Type"].ToString());
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }
    }
}
